package com.zhoutao.blog.seven.service.impl;

import com.zhoutao.blog.seven.dao.model.UserDao;
import com.zhoutao.blog.seven.dao.repository.UserRepository;
import com.zhoutao.blog.seven.model.exception.BaseBizException;
import com.zhoutao.blog.seven.model.exception.ErrorCode;
import com.zhoutao.blog.seven.service.AdminService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Objects;

@Slf4j
@Service
@RequiredArgsConstructor
public class AdminServiceImpl implements AdminService {


    private final UserRepository userRepository;


    @Override
    public Long login(String username, String password) {
        if (!StringUtils.hasText(username) || !StringUtils.hasText(password)) {
            throw new BaseBizException(ErrorCode.USER_PASS_NOT_MATCH);
        }
        UserDao userDao = userRepository.findByUsername(username);
        if (userDao == null) {
            throw new BaseBizException(ErrorCode.USER_PASS_NOT_MATCH);
        }

        if (Objects.equals(userDao.getPsWord(), password)) {
            return userDao.getId();
        }
        throw new BaseBizException(ErrorCode.USER_PASS_NOT_MATCH);
    }
}
